﻿namespace com.stifel.ServerBuildMgmtDao
{
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Xml.Serialization;

    using com.stifel.Extensions.ObjectExtensions;
    using com.stifel.Extensions.StringExtensions;

    [Serializable]
    [XmlRoot( "Log" )]
    public class ActivityAuditLog
    {
        #region Constructors

        public ActivityAuditLog( )
        {
            LogEntries = new List<LogEntry>( );
        }

        #endregion Constructors

        #region Properties

        public List<LogEntry> LogEntries
        {
            get;
            set;
        }

        #endregion Properties

        #region Methods

        public static string AddEntry( string log, string loginName, string entry )
        {
            string LOG_STARTTAG = "<Log>".HtmlEncode( );
            string LOG_ENDTAG = "</Log>".HtmlEncode( );
            string l = string.Empty;
            if ( !string.IsNullOrEmpty( log ) )
            {
                int a = log.IndexOf( LOG_STARTTAG );
                int b = log.IndexOf( LOG_ENDTAG ) + LOG_ENDTAG.Length;
                int c = log.Length;
                l = log.Substring( a, c - a - ( c - b ) );
            }
            ActivityAuditLog auditLog = null;
            if ( !string.IsNullOrEmpty( l ) )
                auditLog = ActivityAuditLog.deserialize( l );
            if ( auditLog == null )
                auditLog = new ActivityAuditLog( );
            auditLog.AddEntry( loginName, entry );
            l = auditLog.serialize( );
            return l;
        }

        public void AddEntry( string by, string entry )
        {
            LogEntries.Add( new LogEntry
            {
                By = by,
                Entry = entry,
                LogDate = DateTime.UtcNow
            } );
        }

        static ActivityAuditLog deserialize( string xmlValue )
        {
            return xmlValue.HtmlDecode( ).DeserializeFromXml<ActivityAuditLog>( );
        }

        string serialize( )
        {
            return this.SerializeToXml<ActivityAuditLog>( ).HtmlEncode( );
        }

        #endregion Methods
    }

    [Serializable]
    public class LogEntry
    {
        #region Properties

        [XmlAttribute( "By" )]
        public string By
        {
            get;
            set;
        }

        [XmlAttribute( "Description" )]
        public string Entry
        {
            get;
            set;
        }

        [XmlAttribute( "Date" )]
        public DateTime LogDate
        {
            get;
            set;
        }

        #endregion Properties
    }
}